package com.ald.crossex.service;

import com.ald.crossex.dao.CeGoodsDao;
import com.ald.crossex.dao.CeGoodsTypeDao;
import com.ald.crossex.dto.JqGridRequest;
import com.ald.crossex.dto.SendOrderDto;
import com.ald.crossex.entity.TCeGoodsType;
import com.ald.crossex.entity.VSendOrder;
import com.ald.crossex.util.JqGridFilterUtils;
import com.ald.crossex.util.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lishijiong on 15/2/5.
 */
@Service
public class GoodsService {
    @Autowired
    private CeGoodsTypeDao goodsTypeDao;
    @Autowired
    private CeGoodsDao goodsDao;
    @Autowired
    private HibernateTemplate hibernateTemplate;

    @Transactional
    public void addGoodsType(TCeGoodsType type) {
        type.setUpdateTime(new Date());
        type.setCreateTime(new Date());
        type.setIsDelete(0);
        goodsTypeDao.create(type);
    }

    @Transactional
    public void updateGoodsType(TCeGoodsType type) {

        TCeGoodsType t = goodsTypeDao.query(type.getTid());
        t.setUpdateTime(new Date());
        t.setName(type.getName());
        t.setSort(type.getSort());
        t.setValid(type.getValid());
        t.setRemark(type.getRemark());
        goodsTypeDao.update(t);

    }

    @Transactional
    public int delGoodsType(String tid){
        String[] ids = tid.split(",");
        int i=0;
        for(;i<ids.length;i++){
            hibernateTemplate.bulkUpdate("update TCeGoodsType set isDelete = 1 where tid = ?",new Object[]{Integer.parseInt(ids[i])});
        }
        return i;

    }

    /**
     * 获取所有的物品类型(前端)
     *
     * @return
     */
    public List<TCeGoodsType> listGoodsType() {
        return goodsTypeDao.queryByHql("from TCeGoodsType order by sort asc", new Object[]{});
    }

    /**
     * 获取所有的货物类型（管理后台）
     *
     * @param request
     * @return
     */
    public Page listGoodsType(JqGridRequest request) {

        Page page = new Page(request.getPage(), request.getRows());

        String conditionSql = " where 1=1 and isDelete = 0 ";

        if (StringUtils.isNotEmpty(request.getFilters())) {
            conditionSql += JqGridFilterUtils.convertJqGridFilter2String(request.getoFilters());
        }

        String orderSql = " order by " + (StringUtils.isEmpty(request.getSidx()) ? " sort " : request.getSidx()) + (StringUtils.isEmpty(request.getSord()) ? " asc " : request.getSord());

        List<TCeGoodsType> list = goodsTypeDao.queryPageByHQL("select  count(*) from TCeGoodsType o " + conditionSql, "from TCeGoodsType o" + conditionSql + orderSql, new Object[]{}, page);

        page.setDatas(list);

        return page;

    }

    /**
     * 获取所有的商品
     *
     * @param request
     * @return
     */
    public Page listGoods(JqGridRequest request) {
        Page page = new Page(request.getPage(), request.getRows());

        String conditionSql = " where 1=1 and isDelete = 0 ";

        if ( StringUtils.isNotEmpty(request.getFilters())) {
            conditionSql += JqGridFilterUtils.convertJqGridFilter2String(request.getoFilters());
        }

        String orderSql = " order by " + (StringUtils.isEmpty(request.getSidx()) ? " gid " : request.getSidx()) + (StringUtils.isEmpty(request.getSord()) ? " desc " : request.getSord());

        List<TCeGoodsType> list = goodsTypeDao.queryPageByHQL("select  count(*) from TCeGoods o " + conditionSql, "from TCeGoods o" + conditionSql + orderSql, new Object[]{}, page);

        page.setDatas(list);

        return page;
    }


}
